package com.dsy.xiangcun_index.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dsy.tools.common.utils.QueryObj;
import com.dsy.tools.entity.TbPhoto;
import com.dsy.tools.entity.TbPhotou;
import com.dsy.tools.entity.TbTou;
import com.dsy.xiangcun_index.service.ITbPhotouService;
import com.dsy.xiangcun_index.service.ITbTouService;
import com.dsy.xiangcun_index.service.ITbPhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dsy
 * @since 2021-03-08
 */
@Controller
@RequestMapping("/photo")
public class TbPhotoController {
    @Autowired
    ITbPhotoService photoService;
    @Autowired
    ITbTouService stuService;

    @Autowired
    ITbPhotouService photouService;
    /**
     * 分页查看风景照列表
     * @param m
     * @param page
     * @param limit
     * @param queryObj
     * @return
     */
    @RequestMapping("/getAllPhoto")
    public String getAllAround(Model m, @RequestParam(defaultValue = "1") long page, @RequestParam(defaultValue = "10") long limit, QueryObj queryObj, HttpSession session){

        QueryWrapper<QueryObj> wrapper = new QueryWrapper<>();

        wrapper.setEntity(queryObj);

        IPage<TbPhoto> photoPage = photoService.getAllPhoto(page,limit,wrapper);
        m.addAttribute("photoPage",photoPage);
        m.addAttribute("page",page);
        m.addAttribute("queryObj",queryObj);
        return "/photo/photo-list";
    }
    /**
     * 根据风景照Id查询
     * @param m
     * @param photoId
     * @return
     */
    @RequestMapping("/getPhotoById")
    public String getMapById(Model m,Integer photoId){
        TbPhoto photo=photoService.getPhotoById(photoId);
        m.addAttribute("photo",photo);
        return "/photo/photo-look";
    }

    @RequestMapping("/getPhotos")
    public String getPhotoAll(Model m){
        List<TbPhoto> photos = photoService.getPhotos();
        for (TbPhoto photo : photos) {
            String url=photo.getPhotoContent();
            Pattern p1=Pattern.compile("\"(.*?)\"");
            Matcher matcher = p1.matcher(url);
            while (matcher.find()) {
//                System.out.println(matcher.group().trim().replace("\"","")+"");
                photo.setPhotoUrl(matcher.group().trim().replace("\"","")+"");
            }
        }
        m.addAttribute("PhotoList",photos);
        return "/photo/photo-all";
    }

    //投票
    @RequestMapping("/addTouToPho")
    @ResponseBody
    public Integer addTouToPho(Integer photoId, HttpSession session){
        TbTou tbTou = (TbTou) session.getAttribute("tbTou");
        TbPhotou tbPhotou=new TbPhotou();
        tbPhotou.setPhoId(photoId);
        tbPhotou.setUserId(tbTou.getUserId());
        List<TbPhotou> photous = photouService.list(new QueryWrapper<TbPhotou>().eq("user_id", tbTou.getUserId()));
        for(TbPhotou photou:photous){
            if (photou.getPhoId().equals(photoId)) {
                return 2;
            }
        }
//        TbPhoto photo = photoService.getPhotoById(photoId);
//        Integer photoLike = photo.getPhotoLike();
//        System.out.println("投票数："+photoLike);
//        photo.setPhotoLike(photoLike+1);
        photoService.updateLikeById(photoId);
        boolean b = photouService.save(tbPhotou);
        if (b){
            return 1;
        }
        return 3;
    }
}
